# HOST = x86_64-linux-gnu

ifndef HOST
CROSS_COMPILE ?= 
HOST = $(shell uname -m)
else
CROSS_COMPILE ?= $(HOST)-
endif

PREFIX ?= $(shell pwd)/build/$(HOST)

MASTERDIR := main
THIRDPARTYLIBRARY := thirdPartyLibrary
LOGSYSTEM := logsystem
SUBFOLDER := $(shell ls -l |awk '/^d/ {print $$NF}' | grep -v $(MASTERDIR) | grep -v "build" | grep -v $(THIRDPARTYLIBRARY) | grep -v $(LOGSYSTEM) ) 

.PHONY: all $(SUBFOLDER) clean doc tag $(THIRDPARTYLIBRARY) $(MASTERDIR) $(LOGSYSTEM)

CFLAGS ?= -include log.h
export CFLAGS

all: $(MASTERDIR) 

clean:
	@for subdir in $(SUBFOLDER) $(MASTERDIR); do \
		$(MAKE) PREFIX=$(PREFIX) HOST=$(HOST) -C $$subdir clean;\
		done

disclean:
	@for subdir in $(THIRDPARTYLIBRARY) $(SUBFOLDER) $(MASTERDIR); do \
		$(MAKE) PREFIX=$(PREFIX) HOST=$(HOST) -C $$subdir clean;\
		done
	@rm -rf $(PREFIX)

thirdLib:$(THIRDPARTYLIBRARY) 

install:
	@for subdir in $(SUBFOLDER) $(MASTERDIR); do \
		$(MAKE) PREFIX=$(PREFIX) HOST=$(HOST) -C $$subdir install;\
		done

test: all
	@for testdir in $(MASTERDIR); do \
		$(MAKE) PREFIX=$(PREFIX) HOST=$(HOST) -C $$testdir test;\
		done

$(MASTERDIR): $(LOGSYSTEM) $(SUBFOLDER) 
	@$(MAKE) CROSS_COMPILE=$(CROSS_COMPILE) PREFIX=$(PREFIX) HOST=$(HOST) -C $@

$(LOGSYSTEM):
	@$(MAKE) CROSS_COMPILE=$(CROSS_COMPILE) PREFIX=$(PREFIX) HOST=$(HOST) -C $@

$(SUBFOLDER) $(THIRDPARTYLIBRARY): $(LOGSYSTEM)
	@$(MAKE) CROSS_COMPILE=$(CROSS_COMPILE) PREFIX=$(PREFIX) HOST=$(HOST) -C $@

doc:
	doxygen doxygen.config

log:
	$(info $(SUBFOLDER))

tag:
	ctags -R --languages=c++ --c-kinds=+p $(SRC_DIR) $(INS_INC) $(INS_DIR)/sample/common/

